function IsNullOrEmpty(val){
	return val == null || val == '';
}

function ExecuteReaderWithSingleCallback(query, db, callback) {
    try {
        //console.log("Executing Query: " + query);
        if (IsNullOrEmpty(query)) {
            callback(null, null);
            return;
        }

        db.transaction(
            function (transaction) {
                transaction.executeSql(query, [], callback, function (transaction, error) {
                    //debugger;
                    //alert("SQL Error: " + error.message);
                    console.log("SQL Error" + error.message);
                    callback(transaction, error);
                });
            }
        );
    }
    catch (e) {throw e; //add this code for testing purposes, so the exception is not suppressed. not in the original.
    }
}

var DriverToUse = "IndexedDB";
var shortName = 'crcInv1';
var version = '1.0';
var displayName = 'CRC Database';
var maxSize = 1024 * 1024 * 5; // in bytes
try{
	var crcDB = openDatabase(shortName, version, displayName, maxSize);
}
catch(e){console.log(e);}


crcDB.transaction(function (tx) {
	var sqlList = [
		"DROP TABLE IF EXISTS tbl1"
	   ,"CREATE TABLE tbl1 (field1 INT)s"
	   ,"INSERT INTO tbl1 (field1) VALUES(1)"
	];
	//select * from stores where chainid=13958 order by cast(storenumber AS int)
	var fns = [];
	for(var i = 0, len = sqlList.length + 10; i < len; i++){

		var sql = (sqlList[i] != undefined) ? sqlList[i] :
					sqlList[sqlList.length-1];
	
		var fn = function(sql, callback){
		
			return function(callback){
				console.log(sql);
				tx.executeSql(sql, [], 
					function (tx, result) {
						console.log(result);
						callback();
					}
					,function (tx, e) {
						console.log(e);
						//callback();
						//throw new Error('a');
					}
				);			
			}
			
		}.apply(this, [sql, function(){}]);
		
		fns = fns.concat([fn]);
		
	}
	
	async.series(fns, function(err, results){})
});

/*
ExecuteReaderWithSingleCallback("SELECT * FROM non_existing", crcDB);

ExecuteReaderWithSingleCallback("SELECT * FROM non_existing", crcDB, function(){
	console.log('b');
});

ExecuteReaderWithSingleCallback("SELECT * FROM non_existing", crcDB, function(){
	console.log('c');
});

ExecuteReaderWithSingleCallback("SELECT * FROM non_existing", crcDB, function(){
	console.log('d');
});
*/
/*
query="drop table if exists a";
ExecuteReaderWithSingleCallback(query, crcDB, function(){
	
	//console.log(crcDB.getStorageDriver()._dbData);
	//console.log(crcDB.getStorageDriver()._schemaData);
	
	query = "CREATE TABLE if not exists a (field1 integer primary key autoincrement, field2 text)";
	ExecuteReaderWithSingleCallback(query, crcDB, function(){
		//console.log(crcDB.getStorageDriver()._dbData);
		//console.log(crcDB.getStorageDriver()._schemaData);		
		query = "INSERT INTO a(field2) VALUES('a')";
		ExecuteReaderWithSingleCallback(query, crcDB, function(){
			//console.log(crcDB.getStorageDriver()._dbData);
			//console.log(crcDB.getStorageDriver()._schemaData);
			query = "INSERT INTO a(field2) VALUES('b')";
			ExecuteReaderWithSingleCallback(query, crcDB, function(){
				//console.log(crcDB.getStorageDriver()._dbData);
				//console.log(crcDB.getStorageDriver()._schemaData);
				query = "SELECT * FROM a WHERE field1 = 2";
				ExecuteReaderWithSingleCallback(query, crcDB, function(t, r_or_e){
					console.log(r_or_e);					
					query = "UPDATE a SET field2 = 'e' WHERE field1 = 2";
					ExecuteReaderWithSingleCallback(query, crcDB, function(t, r_or_e){
						console.log(r_or_e);
						query = "SELECT * FROM a order by field1 collate nocase asc";
						ExecuteReaderWithSingleCallback(query, crcDB, function(t, r_or_e){
							console.log(r_or_e);
							query = "DELETE FROM a WHERE field1 = 2";
							ExecuteReaderWithSingleCallback(query, crcDB, function(t, r_or_e){
								console.log(r_or_e);
								query = "SELECT * FROM a";
								ExecuteReaderWithSingleCallback(query, crcDB, function(t, r_or_e){
									console.log(r_or_e);
								});
							});							
						});
					});					
				});
			});
		});		
	});
	
});

*/

/*
crcDB.transaction(function (tx) {
	var sqlList = [
		"DROP TABLE IF EXISTS tbl1"
	   ,"CREATE TABLE tbl1 (field1 INT)"
	   ,"INSERT INTO tbl1 (field1) VALUES(1)"
	];
	//select * from stores where chainid=13958 order by cast(storenumber AS int)
	for(var i = 0, len = sqlList.length + 10; i < len; i++){
		
		var sql = (sqlList[i] != undefined) ? sqlList[i] :
						sqlList[sqlList.length-1];
	
		//console.log('a1');
		tx.executeSql(sql, [], 
			function (tx, result) {
				//console.log("OK");
				//console.log(tx.sql);
				console.log(result);
			}
			,function (tx, e) {
				//console.log("failed");
				console.log(e);
			}
		);	
		//console.log('a');
		
	}
	
});
*/

/*
crcDB.transaction(function(trxn){
	trxn.executeSql(
		query
	   ,[]
	   ,function(t, r){
			console.log(crcDB.getStorageDriver()._dbData);
			console.log(crcDB.getStorageDriver()._schemaData);

			query="CREATE TABLE if not exists a (field1 int)";
			crcDB.transaction(function(trxn){
				trxn.executeSql(
					query
				   ,[]
				   ,function(t, r){
						console.log(crcDB.getStorageDriver()._dbData);
						console.log(crcDB.getStorageDriver()._schemaData);						
				   }
				   ,function(t, e){
						console.log(e);
				   }
				);
			});
	   }
	   ,function(t, e){
			console.log(e);
	   }
	);
});



crcDB.transaction(function (tx) {
	var sqlList = [
		"DROP TABLE IF EXISTS tbl1"
	   ,"CREATE TABLE tbl1 (field1 INT)"
	   ,"INSERT INTO tbl1 (field1) VALUES(1)"
	];
	//select * from stores where chainid=13958 order by cast(storenumber AS int)
	for(var i = 0, len = sqlList.length + 2999; i < len; i++){
		
		var sql = (sqlList[i] != undefined) ? sqlList[i] :
						sqlList[sqlList.length-1];
	
		//console.log('a1');
		tx.executeSql(sql, [], 
			function (tx, result) {
				//console.log("OK");
				//console.log(tx.sql);
				console.log(result);
			}
			,function (tx, result) {
				console.log("failed");
				//console.log('a');
			}
		);	
		//console.log('a');
		
	}
	
});
*/
/*
var shortName = 'crcInv1';
var version = '1.0';
var displayName = 'CRC Database';
var maxSize = 1024 * 1024 * 5; // in bytes
var crcDB = openDatabase(shortName, version, displayName, maxSize);

function IsNullOrEmpty(val){
	return val == null || val == '';
}

function ExecuteReaderWithSingleCallback(query, db, callback) {
    try {
        console.log("Executing Query: " + query);
        if (IsNullOrEmpty(query)) {
            callback(null, null);
            return;
        }

        db.transaction(
            function (transaction) {
                transaction.executeSql(query, [], callback, function (transaction, error) {
                    //debugger;
                    //alert("SQL Error: " + error.message);
                    console.log("SQL Error" + error.message);
                    callback(transaction, error);
                });
            }
        );
    }
    catch (e) {throw e; //add this code for testing purposes, so the exception is not suppressed. not in the original.
    }
}


var query = "CREATE TABLE a (field1 int)";
ExecuteReaderWithSingleCallback(query, crcDB, function(t, r_or_e){
});

				
var execBelow = false;
if( execBelow ){

var key = null;
// Simply open the database once so that it is created with the required tables
$.indexedDB("MyECommerceSite", {
	"schema": {
		"1": function(versionTransaction){
			var catalog = versionTransaction.createObjectStore("catalog", {
				"keyPath": "itemId"
			});
			catalog.createIndex("price");
		}		
	}
}).done(function(){
	// Once the DB is opened with the object stores set up, show data from all tables
	// window.setTimeout(function(){
		// loadFromDB("cart");
		// loadFromDB("wishlist");
		// downloadCatalog();
	// }, 100)
});

}
*/